package com.zhuss.excecise.processor;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.excecise.excecise.DeleteExceciseAppendBo;
import com.zhuss.bean.bo.excecise.excecise.wechat.CollectExceciseBo;
import com.zhuss.bean.vo.excecise.excecise.DetailExceciseAppendVo;
import com.zhuss.bean.vo.excecise.excecise.DetailExceciseVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 取消收藏练习生 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("exceciseDelCollect")
public class ExceciseDelCollect extends AbstractProcessor<Object, CollectExceciseBo> implements Processor<Object, CollectExceciseBo> {

    @Autowired
	@Qualifier("exceciseRead")
	private ExceciseRead exceciseRead;
    
    @Autowired
	@Qualifier("exceciseAppendRead")
	private ExceciseAppendRead exceciseAppendRead;
    
    @Autowired
    @Qualifier("exceciseAppendDelete")
    private Processor<Object, DeleteExceciseAppendBo> exceciseAppendDelete;
    
    /**
     * 校验
     */
    @Override
    public void check(CollectExceciseBo bo) throws WpsException {
    	Long exceciseId = bo.getExceciseId();
    	Integer appendType = bo.getAppendType();
    	
    	DetailExceciseAppendVo result = exceciseAppendRead.getByType(appendType, exceciseId, bo.getConcurrentUserId());
    	
    	if (result == null) {
    		throw new WpsException(ExceptionCode.练习生未收藏);
    	}
    	
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(CollectExceciseBo param) throws WpsException {
    	Long exceciseId = param.getExceciseId();
    	Integer appendType = param.getAppendType();
    	
    	DetailExceciseAppendVo result = exceciseAppendRead.getByType(appendType, exceciseId, param.getConcurrentUserId());
    	
    	DetailExceciseVo excecise = ((DetailExceciseVo) exceciseRead.detail(exceciseId));
    	String name = excecise.getTitle();
    	
    	DeleteExceciseAppendBo bo = new DeleteExceciseAppendBo();
    	BeanUtils.copyProperties(param, bo);
    	bo.setExceciseAppendId(result.getExceciseAppendId());
    	exceciseAppendDelete.process(bo);
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CollectExceciseBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.练习生.key());
    	record.setAction(LogActionEnum.练习生取消收藏.key());
    	record.setTargetId(param.getExceciseId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
